// pages/user-profile-edit/user-profile-edit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '天河区'],

  },
  petname(e) {
    this.setData({
      petname: e.detail.value
    })
  },
  phone(e) {
    this.setData({
      phone: e.detail.value,
      phonelength: e.detail.value.length
    })
  },
  bindAddressChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  radioChange(e) {
    // console.log("单选框的值：", e.detail.value)
    this.setData({
      gender: e.detail.value
    })

  },
  bindSignature: function (e) {
    // console.log(e.detail.value)
    this.setData({
      signature: e.detail.value
    })
  },
  addchooseImage() {
    wx.chooseImage({
      success: res => {
        // console.log(res)
        this.setData({
          chooseImage: res.tempFilePaths[0]
        })
      }
    })
  },

  edit() {
    var id = this.data.arraa[0].id
    if (this.data.phonelength < 11) {
      console.log("手机号码的长度不正确")
    } else {
      var that = this;
      wx.request({
        url: 'http://localhost/shopping/users_edit.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          id: id,
          img: that.data.chooseImage,
          gender: that.data.gender,
          petname: that.data.petname,
          phone: that.data.phone,
          address: that.data.region,
          signature: that.data.signature
        },
        success: function (res) {
          // console.log(res.data)
          console.log("个人资料修改成功！")
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({})
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    }

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var name = e.name
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/users.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        that.setData({
          arraa: res.data.users,
          chooseImage: res.data.users[0].img
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

    that.setData({
      name: name,

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})